package com.tboss.template.home.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.tboss.template.common.springSecurity.SecurityService;
import com.tboss.template.common.springSecurity.SecurityUserDto;
import com.tboss.template.common.type.WidgetType;
import com.tboss.template.system.dto.RoleDto;
import com.tboss.global.utils.securities.algorithm.TBCrypt;

@Service
public class AuthService {
	
	@Autowired private SecurityService securityService;
	@Autowired private SecurityContextRepository repository;
	private final String SUFFIX = WidgetType.EnSubffix.getValue();

	private void setAuthentication(String decName, HttpServletRequest req, HttpServletResponse res){
		String lastName = "";
		if("widgetUser01".equals(decName)){ lastName = "위젯사용자 01";
		}else if("widgetUser02".equals(decName)){ lastName = "위젯사용자 02";
		}else if("widgetUser03".equals(decName)){ lastName = "위젯사용자 03";
		}else if("widgetUser04".equals(decName)){ lastName = "위젯사용자 04";
		}else{ lastName = "위젯사용자 X-man";
		}
		
		SecurityUserDto user = new SecurityUserDto();
		user.setUsername(decName);
		user.setLastName(lastName);
		
        //ROLE 등록.
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    	List<RoleDto> roles = this.securityService.getRoles(user.getUsername());
    	for(RoleDto role : roles){
    		authorities.add(new SimpleGrantedAuthority(role.getRoleId()));
    	}
        user.setAuthorities(authorities);
        
        //메뉴등록
        user.setMyMenu(this.securityService.getMyMenu(user.getUsername()));
		
		//AuthorityUtils.createAuthorityList("ROLE_USER")
		Authentication authentication = 
				new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
		SecurityContextHolder.getContext().setAuthentication(authentication);		
		this.repository.saveContext(SecurityContextHolder.getContext(), req, res);
	}
	
	public void uplusOssWidgetLogin(ModelMap modelMap, String encName, String decName,  
			HttpServletRequest req, HttpServletResponse res){
		
		Boolean chkSucc = false;
		String decrypt = null;		
		TBCrypt tbCrypt = new TBCrypt();
		tbCrypt.setUserKey(tbCrypt.tbCryptDate(0));
		decrypt = tbCrypt.decrypt(encName.replace(" ", "+"));		
		if(decrypt.endsWith(this.SUFFIX)){
			decrypt = decrypt.substring(0, decrypt.lastIndexOf(this.SUFFIX));
			if(decName.equals(decrypt)){
				this.setAuthentication(decName, req, res);
				chkSucc = true;
			}
		}
		
		modelMap.put("chkSucc", chkSucc);
	}
	
}
